package utils;

import com.google.common.base.Optional;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

/**
 * User: Darren
 * Date: 25/04/13
 * Time: 12:00 PM
 */
public class FileUtilities {
    private static Logger logger = Logger.getLogger(FileUtilities.class);
    public static String readFileToString(Object caller, String fileName) throws IOException {
        StringWriter writer = new StringWriter();
//        Optional<InputStream> streamOptional = Optional.fromNullable(caller.getClass().getResourceAsStream("/" + fileName));
        Optional<InputStream> streamOptional = Optional.fromNullable(caller.getClass().getResourceAsStream(fileName));
        if (!streamOptional.isPresent()) {
            logger.error("The file '" + fileName + "' was not found");
            return "";
        }
        IOUtils.copy(streamOptional.get(), writer);
        return writer.toString();
    }
}
